﻿
namespace MTechClient
{
    using System;
    using System.Globalization;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.Windows.Forms;
    using MTechClient.Properties;
    using System.Diagnostics;
    using MTechClient.SubmitApp;

    public partial class MTechForm : Form
    {
        MTechClient.SubmitApp.ServiceClient proxy; 
        delegate void SetTextCallback(string text);

        public MTechForm()
        {
            this.InitializeComponent();
            SetFieldDefaults();
        }

        public void SetConfirmationText(string text)
        {
            this.Invoke(
                new MethodInvoker(() =>
                    {
                        this.confirmationTitle.Text = text;
                        Debug.WriteLine("*** Setting confirmation text = " + text);
                        this.confirmationPanel.Visible = true;
                        this.confirmationPanel.Update();
                    }));
        }

        private SubmitMTechApplicationRequest CreateApplicationRequest()
        {
            return new SubmitMTechApplicationRequest()
            {
                Info = new ApplicantInfo()
                {
                    Name = this.txtName.Text,
                    Email = this.txtEmail.Text,
                    IDNo = this.txtIDNo.Text,
                    HighestDegree = this.comboBoxEducation.Text,
                    Program = this.comboBoxProgram.Text,
                    Experience = String.IsNullOrEmpty(txtExperience.Text) ? 0 : Int32.Parse(this.txtExperience.Text.Trim(), CultureInfo.CurrentCulture),
                    MediumOfInstruction = this.comboBoxMediumOfInstruction.Text,
                    EnglishTestType = this.comboBoxEnglishTestType.Text,
                    EnglishTestScore = String.IsNullOrEmpty(txtEnglishTestScore.Text) ? 0 : Int32.Parse(this.txtEnglishTestScore.Text.Trim(), CultureInfo.CurrentCulture),
                    
                },
                RequestID = Guid.NewGuid()
            };
        }

        private void SubmitMTechApplication()
        {
            ShowConfirmationPanel();
            proxy = new MTechClient.SubmitApp.ServiceClient();
            proxy.BeginSubmitInfo(CreateApplicationRequest(), OnStartCompleted, null);
        }

        private void OnStartCompleted(IAsyncResult asr)
        {
            try
            {
                SubmitMTechApplicationResponse result = (SubmitMTechApplicationResponse)proxy.EndSubmitInfo(asr);
                SetConfirmationText(result.ResponseText);
                ShowStartAgainButton();
                proxy.Close();
            }
            catch (CommunicationException)
            {
                ShowExceptionText(Resources.CommException);
                proxy.Abort();
            }
            catch (TimeoutException)
            {
                ShowExceptionText(Resources.TimeoutText);
                proxy.Abort();
            }
            catch (Exception)
            {
                proxy.Abort();
                throw;
            }
        }

        private void ShowExceptionText(string text)
        {
            SetConfirmationText(text);
            ShowStartAgainButton();
        }

        private void ShowStartAgainButton()
        {
            this.Invoke(
                new MethodInvoker(() =>
                {
                    this.buttonStartAgain.Visible = true;
                }));
        }

        private void ShowConfirmationPanel()
        {
            this.confirmationPanel.Visible = true;
            this.buttonStartAgain.Visible = false;
            this.confirmationTitle.Text = Resources.SubmitText;
            this.Update();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            SubmitMTechApplication();
        }

        private void buttonStartAgain_Click(object sender, EventArgs e)
        {
            SetFieldDefaults();
            this.confirmationPanel.Visible = false;
        }

        private void SetFieldDefaults()
        {
            Random r = new Random();

            if (r.Next() % 2 == 0)
                this.txtName.Text = "Tom";
            else
                this.txtName.Text = "Jerry";
            this.txtExperience.Text = "5";
            this.txtEmail.Text = "tj@example.com";
            this.txtIDNo.Text = "S1234567A";
            this.comboBoxEducation.Text = r.Next() % 2 == 0 ? "Bachelors" : "Masters";
            this.comboBoxProgram.Text = r.Next() % 2 == 0 ? "Computer Science" : "Art";
            this.comboBoxMediumOfInstruction.Text = "Chinese";
            this.comboBoxEnglishTestType.Text = "IELTS";
            txtEnglishTestScore.Text = "6";        

        }
       
    }
}
